package gererg;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author leon(liangzou0318@gmail.com)
 * @date 2012-12-4
 * @filaname ComparaDemos.java
 */
public class ComparaDemos {
    
	public void getIntsort(){
		int a[]={3,1,4,7,2,9};
		Arrays.sort(a);
		for(int i=0;i<6;i++){
			System.out.print(a[i]+"  ");
	  }
	}
	public void getStrsort(){
		String b[]={"vgwg","qwfg","rweh","vbewh"};
		Arrays.sort(b);
		for(int i=0;i<b.length;i++){
			System.out.print(b[i]+"  ");
		}
	}
	private class Student{
		private String name;
		private int age;
	    public Student(String name,int age){
		   this.name=name;
		   this.age=age;
	   }
	    public String getName(){
	    	return name;
	    }
	    public int getAge(){
	    	return age;
	    }
	    public void setName(String name){
	    	this.name=name;
	    }
	    public void setAge(){
	    	this.age=age;
	    }
		
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ComparaDemos cd=new ComparaDemos();
		cd.getIntsort();
		cd.getStrsort();
		cd.getObjetsort();
	}
	public class ByAge implements Comparator{

		/* (non-Javadoc)
		 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
		 */
		@Override
		public int compare(Object o1, Object o2) {
			Student t1=(Student)o1;
			Student t2=(Student)o2;
			if (t1.age>t2.age)return 1;
			else return 0;
			
		}
		
	} 
	private void getObjetsort(){
		  Student st1=new Student("a",22);
		  Student st2=new Student("b",21);
		  Student st3=new Student("c",26);
		  Student st4=new Student("d",19);
		  Student st5=new Student("e",20);
		  Student st6=new Student("g",18);
		  Student[] students={st1,st2,st3,st4,st5,st6};
		  Arrays.sort(students,new ByAge()); //   sort By age
		  for(Student ss : students){
		      
			System.out.print(ss.getName()+"    ");
		  }
		}
	
}
